package com.qf.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class BaseServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Class clazz = this.getClass();//获取子类字节码文件对象
        try {
            String methodName = request.getParameter("methodName");
            if(methodName == null){
                methodName="ShowProduct";
            }
            Object obj = clazz.newInstance();//创建子类对象
            Method method = clazz.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(obj,request,response);//调用方法
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public  void ShowProduct(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        System.out.println("加载");
    }
}
